package com.mobisys.live.controller.statisticcontroller;

import com.mobisys.live.bean.channel.GetChannelList;
import com.mobisys.live.bean.live.GetLiveInfo;
import com.mobisys.live.dao.mapper.*;
import com.mobisys.live.dao.model.*;
import com.mobisys.live.response.Response;
import com.mobisys.live.response.statistic.LiveInfo;
import com.mobisys.live.response.statistic.LiveStatisticResponseBody;
import com.mobisys.live.response.statistic.UserChannelAndLiveResponseBody;
import com.mobisys.live.response.statistic.UserLiveRecordResponseBody;
import com.mobisys.live.service.UserLiveWatchingRecordService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;


/*
* liu
* */
@RestController
public class LiveRecordController {

    @Autowired
    private UserLiveWatchingRecordService userLiveWatchingRecordService;
    private Logger logger = LogManager.getLogger(LiveRecordController.class.getName());

    @Autowired
    private LiveStatisticMapper liveStatisticMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LiveMapper liveMapper;

    @Autowired
    private ChannelMapper channelMapper;
    /*
    * 获取直播观看人数的相关信息
    * 匿名用户 anonymous_users
    * */
    @RequestMapping(value = "/live/record",method = RequestMethod.POST)
    public Response getLiveRecord(@RequestBody UserLiveWatchingRecord userLiveWatchingRecord ){
        Response response = new Response();
       if(userLiveWatchingRecord == null){
           logger.debug("userLiveWatchingRecord获取失败");
           response.setRetureValue(RERCORD_LIVE_ERROR);
           response.setRetureMsg(MSG_RERCORD_LIVE_ERROR);
           return response;
       }
       int key = userLiveWatchingRecordService.insert(userLiveWatchingRecord);
       if(key <= 0){
           logger.debug("userLiveWatchingRecord插入失败");
           response.setRetureValue(RERCORD_LIVE_ERROR);
           response.setRetureMsg(MSG_RERCORD_LIVE_ERROR);
           return response;
       }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        return response;

    }

    /**
     * 直播人数、质量信息统计
     * @param getLiveInfo
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/live/liveStatistics",method = RequestMethod.POST)
    public Response liveStatistics(@RequestBody GetLiveInfo getLiveInfo,HttpServletRequest httpServletRequest){
        Response response = new Response<List<LiveStatisticResponseBody>>();

        List<LiveStatistic> liveStatisticList = liveStatisticMapper.selectLiveStatisticByLiveId(getLiveInfo.getLiveId());

        if(liveStatisticList.size()==0||liveStatisticList==null){
            response.setRetureValue(DATABASE_ERROR);
            response.setRetureMsg(MSG_DATABASE_ERROR);
            return response;
        }

            List<LiveStatisticResponseBody> liveStatisticResponseBodyList = new ArrayList<LiveStatisticResponseBody>();
            for (LiveStatistic liveStatistic : liveStatisticList) {
                LiveStatisticResponseBody liveStatisticResponseBody = new LiveStatisticResponseBody();
                liveStatisticResponseBody.setId(liveStatistic.getId());
                liveStatisticResponseBody.setLiveId(liveStatistic.getLiveId());
                liveStatisticResponseBody.setFps(liveStatistic.getFps());
                liveStatisticResponseBody.setBps(liveStatistic.getBps());
                liveStatisticResponseBody.setBandwidth(liveStatistic.getBandwidth());
                liveStatisticResponseBody.setOnline(liveStatistic.getOnline());
                liveStatisticResponseBody.setTime(liveStatistic.getTime());
                liveStatisticResponseBodyList.add(liveStatisticResponseBody);
            }

            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
            response.setRetureData(liveStatisticResponseBodyList);
            return response;

    }

    /**
     * 获得观看某一直播的所有用户
     * @param getLiveInfo
     * @return
     */
    @RequestMapping(value = "/live/userListStatistics",method = RequestMethod.POST)
    public Response userListStatistics(@RequestBody GetLiveInfo getLiveInfo){
        Response response = new Response<UserLiveRecordResponseBody>();

        List<UserLiveWatchingRecord> userLiveWatchingRecordList = userLiveWatchingRecordService.selectListByLiveId(getLiveInfo.getLiveId());
        if(userLiveWatchingRecordList.size()==0||userLiveWatchingRecordList==null){
            response.setRetureValue(DATABASE_ERROR);
            response.setRetureMsg(MSG_DATABASE_ERROR);
            return response;
        }
        List<UserLiveRecordResponseBody> userLiveRecordResponseBodyList = new ArrayList<UserLiveRecordResponseBody>();
        for(UserLiveWatchingRecord userLiveWatchingRecord:userLiveWatchingRecordList){
            UserLiveRecordResponseBody userLiveRecordResponseBody = new UserLiveRecordResponseBody();
            userLiveRecordResponseBody.setUserId(userLiveWatchingRecord.getUserId());
            if(!userLiveWatchingRecord.getUserId().equals("anonymous_users")) {
                User user = userMapper.selectByPrimaryKey(userLiveWatchingRecord.getUserId());
                if (user==null){
                    response.setRetureValue(DATABASE_ERROR);
                    response.setRetureMsg(MSG_DATABASE_ERROR);
                    return response;
                }else {
                    userLiveRecordResponseBody.setUserName(user.getUsername());
                }
            }else {
                userLiveRecordResponseBody.setUserName("匿名用户");
            }
            userLiveRecordResponseBody.setUa(userLiveWatchingRecord.getUa());
            userLiveRecordResponseBody.setVisitIp(userLiveWatchingRecord.getVisitIp());
            userLiveRecordResponseBody.setVisitTime(userLiveWatchingRecord.getVisitTime());
            userLiveRecordResponseBody.setLeaveTime(userLiveWatchingRecord.getLeaveTime());
            userLiveRecordResponseBodyList.add(userLiveRecordResponseBody);
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(userLiveRecordResponseBodyList);
        return response;
    }


    @RequestMapping(value = "/statistic/getChannelAndLive",method = RequestMethod.POST)
    public Response getChannelAndLive(@RequestBody GetChannelList getChannelList){
        Response response = new Response();

        List<Channel> channelList = channelMapper.selectByUserId(getChannelList.getUserId());
        if(channelList.size()==0){
            response.setRetureValue(NOT_LIVE);
            response.setRetureMsg((MSG_NOT_LIVE));
            return response;
        }
        List<UserChannelAndLiveResponseBody> userChannelAndLiveResponseBodyList = new ArrayList<UserChannelAndLiveResponseBody>();
        for(Channel channel:channelList){
            List<LiveWithBLOBs> liveList = liveMapper.selectLiveListByChannelid(channel.getId());
//            if(liveList.size()==0){
//                response.setRetureValue(DATABASE_ERROR);
//                response.setRetureMsg(MSG_DATABASE_ERROR);
//                return response;
//            }
//            List<String> liveIdList = new ArrayList<String>();
//            List<String> liveTitleList = new ArrayList<String>();
            UserChannelAndLiveResponseBody userChannelAndLiveResponseBody = new UserChannelAndLiveResponseBody();
            userChannelAndLiveResponseBody.setChannelId(channel.getId());
            userChannelAndLiveResponseBody.setChannelName(channel.getChannelName());
            List<LiveInfo> liveInfoList = new ArrayList<LiveInfo>();
            for(int i=0;i<liveList.size();i++){
//                liveIdList.add(liveList.get(i).getId());
//                liveTitleList.add(liveList.get(i).getTitle());
                LiveInfo liveIdAndTitle = new LiveInfo();
                liveIdAndTitle.setLiveId(liveList.get(i).getId());
                liveIdAndTitle.setLiveTitle(liveList.get(i).getTitle());
                liveInfoList.add(liveIdAndTitle);
            }
//            userChannelAndLiveResponseBody.setLiveId(liveIdList);
//            userChannelAndLiveResponseBody.setLiveTitle(liveTitleList);
            userChannelAndLiveResponseBody.setLiveInfo(liveInfoList);
            userChannelAndLiveResponseBodyList.add(userChannelAndLiveResponseBody);
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(userChannelAndLiveResponseBodyList);
        return response;
//        for(LiveWithBLOBs liveWithBLOBs:liveWithBLOBsList){
//            if(userChannelAndLiveResponseBodyList.size()!=0){
//                for(int i=0;i<userChannelAndLiveResponseBodyList.size();i++){
//                    if(userChannelAndLiveResponseBodyList.get(i).getChannelId().contains(liveWithBLOBs.getChannelid())){
//                        userChannelAndLiveResponseBodyList.get(i).getLiveId().add(liveWithBLOBs.getId());
//                        userChannelAndLiveResponseBodyList.get(i).getLiveTitle().add(liveWithBLOBs.getTitle());
//                        continue A;
//                    }
//                }
//            }else {
//                UserChannelAndLiveResponseBody userChannelAndLiveResponseBody = new UserChannelAndLiveResponseBody();
//                userChannelAndLiveResponseBody.setChannelId(liveWithBLOBs.getChannelid());
//                userChannelAndLiveResponseBody.setChannelName(channelMapper.sel);
//            }
//        }

    }
}
